﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Interface;
using Domain;
using Clannan.Models.JsonModel;

namespace Clannan.Controllers
{
    public class MobilePageController : Controller
    {
        public IMobilePageService MobilePageService;

        /// <summary>
        /// 获取手机页分页集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public JsonResult GetByPage(string pageIndex, string pageSize)
        {
            List<MobilePageJsonModel> mobilepageList = new List<MobilePageJsonModel>();//Json返回页集合
            MobilePageJsonModel mobilepageJsonModel; //Json模型
            int pageCount;               //总页数

            //获取分页数据
            IList<MobilePage> mobilepageListData = this.MobilePageService
                .LoadAllMobilePageByPage(Convert.ToInt32(User.Identity.Name), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount, false);
            //数据处理
            foreach (MobilePage mobilepage in mobilepageListData)
            {
                mobilepageJsonModel = new MobilePageJsonModel();
                mobilepageJsonModel = mobilepageJsonModel.Copy(mobilepageJsonModel, mobilepage);
                mobilepageList.Add(mobilepageJsonModel);
            }

            return Json(new { Message = 1, MobilePageList = mobilepageList, PageCount = pageCount });
        }

        /// <summary>
        /// 获取手机页
        /// </summary>
        /// <param name="mobilepageID">手机页ID</param>
        /// <returns></returns>
        public JsonResult Get(string mobilepageID)
        {
            MobilePage mobilepage = this.MobilePageService.Get(mobilepageID);
            var result = new
            {
                ID = mobilepage.ID,
                Name = mobilepage.Name,
                Conteng = mobilepage.Content,
                CreateTime = mobilepage.CreateTime,
                UpdateTime = mobilepage.UpdateTime
            };

            return Json(result);
        }

        /// <summary>
        /// 添加手机页
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public JsonResult Create(string name, string content)
        {
            MobilePage mobilepage = new MobilePage();
            mobilepage.Name = name;
            mobilepage.Content = content;
            mobilepage.CreateTime = DateTime.Now;
            mobilepage.MarkDelete = false;
            mobilepage.UserInfo = new UserInfo { ID = Convert.ToInt32(User.Identity.Name) };
            this.MobilePageService.Save(mobilepage);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 修改手机页
        /// </summary>
        /// <param name="mobilepageID">手机页ID</param>
        /// <param name="name">手机页名称</param>
        /// <param name="content">手机页内容</param>
        /// <returns></returns>
        public JsonResult Update(string mobilepageID, string name, string content)
        {
            MobilePage mobilepage = this.MobilePageService.Get(mobilepageID);
            mobilepage.Name = name;
            mobilepage.Content = content;
            mobilepage.UpdateTime = DateTime.Now;
            this.MobilePageService.Save(mobilepage);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 将手机页放入垃圾箱
        /// </summary>
        /// <param name="mobilepageID">手机页ID</param>
        /// <returns></returns>
        public JsonResult Throw(string mobilepageID)
        {
            this.MobilePageService.UpdateMobilePageMarkDelete(mobilepageID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原手机页
        /// </summary>
        /// <param name="mobilepageID">手机页ID</param>
        /// <returns></returns>
        public JsonResult Restore(string mobilepageID)
        {
            this.MobilePageService.UpdateMobilePageMarkDelete(mobilepageID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 删除手机页
        /// </summary>
        /// <param name="mobilepageID">手机页ID</param>
        /// <returns></returns>
        public JsonResult Delete(string mobilepageID)
        {
            this.MobilePageService.Delete(mobilepageID);

            return Json(new { Message = 1 });
        }
    }
}
